home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 16874 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.9 KB

  1. Path: news.mty.itesm.mx!academ07!al522331
  2. From: al522331@academ07.mty.itesm.mx (RAFAEL ALGARA TORRE)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: easy c++ question
  5. Date: 12 Apr 1996 16:33:37 GMT
  6. Organization: ITESM Campus Monterrey . DINF-DTCI
  7. Message-ID: <4km0l1$kiu@news.mty.itesm.mx>
  8. References: <316901DA.3138C677@ablecom.net>
  9. NNTP-Posting-Host: academ07.mty.itesm.mx
  10. X-Newsreader: TIN [version 1.2 PL2]
  11.  
  12. The Letter O (jczech@ablecom.net) wrote:
  13. : Okay, I'm a newbie in C++, and am currently working through
  14. : Practical C++ programming by O'Reilly & Associates. I'm working
  15. : on my first C++ program with classes, and the objective is to 
  16. : make a simple stack class with member functions, a constructor
  17. : and a destructor. simple right? 
  18.  
  19. : //======================================================
  20. : class stack {
  21. :     private:
  22. :         int count;       // number of items in the stack
  23. :         int data[100];   // the items themselves
  24. :     public:
  25. :     .
  26. :     .
  27. :     .
  28. : //======================================================
  29.  
  30.  
  31. : I was using the following init() member function to initialize
  32. : my stack variables:
  33.  
  34. : //=======================================
  35. : inline void stack::init(void)
  36. : {
  37. :         count = 0;      // zero the stack
  38. : }
  39. : //=======================================
  40.  
  41. : Simple, right? right. Then I modified it and made it into a 
  42. : constructor, so that it would automatically be called whenever
  43. : I declared a variable of type: stack  as follows:
  44.  
  45. : //==============================================
  46. : inline stack::stack(void)
  47. : {
  48. :         count = 0;      // zero the stack
  49. :         cout << "constructor has been called\n";
  50. : }
  51. : //==============================================
  52.  
  53. : When I changed it to the constructor (above) it was not called
  54. : automatically when I declared a variable of type: stack, and 
  55. : thus the stack counter wasn't initialized, and I got a SIGSEGV
  56. : signal because count starts out with a junk value that's beyond
  57. : the bounds of the data array of my stack variable.
  58.  
  59. : My point is: Aren't constructors automagically called when a variable
  60. : is declared, simply because they have the same name as the class? 
  61. : ie: stack::stack(void)
  62.  
  63.  
  64. : I'm using G++ 2.7.0 under Linux slackware 3.0, and Kernel 1.2.13
  65. : Is there some compiler switch to enable constructors? Is there a bug in
  66. : this version of g++? Am I just a moron who should be banished from programming? 
  67.  
  68. : Thanks for any help provided...
  69.  
  70. : *Jczech@ablecom.net
  71.  
  72. : Replies by email and/or newsgroup are appreciated.
  73.  
  74.  
  75. --
  76. I haven't ever worked on the platform you do I use BC++. But why do you write inline
  77. constructors anyway? A far as I understand, polymorphic objects resolve their
  78. construction sequence at runtime, so their constructors can't be inline, or at least, there's no reason to write them as inline.
  79.  
  80.  
  81. ------------------------------------------
  82. Rafael Algara Torre
  83. ITESM Monterrey, Mexico
  84. al522331@academ01.mty.itesm.mx
  85. ------------------------------------------
  86.